<?php
//var_dump($monthBudget);

$headerRow = '<th>Libellés</th>';
$nbopendaysRow = '<td class="itemLibelle">Nombre de jours d\'ouverture - mois</td>';
$nbWeek1Row = '<td class="itemLibelle">Nombre de jours d\'ouverture - semaine 1</td>';
$nbWeek2Row = '<td class="itemLibelle">Nombre de jours d\'ouverture - semaine 2</td>';
$nbWeek3Row = '<td class="itemLibelle">Nombre de jours d\'ouverture - semaine 3</td>';
$nbWeek4Row = '<td class="itemLibelle">Nombre de jours d\'ouverture - semaine 4</td>';
$nbWeek5Row = '<td class="itemLibelle">Nombre de jours d\'ouverture - semaine 5</td>';
$nbWeek6Row = '<td class="itemLibelle">Nombre de jours d\'ouverture - semaine 6</td>';
$totttcJRow = '<td class="itemLibelle">Total Chiffre d\'Affaires TTC/Jour</td>';
$totttcRow = '<td class="itemLibelle">Total Chiffre d\'Affaires TTC</td>';
$tothtRow = '<td class="itemLibelle">Total Chiffre d\'Affaires HT</td>';
$transactionRow = '<td class="itemLibelle">Transation / Mensuelle</td>';
$tmJRow = '<td class="itemLibelle">Ticket Moyen T.T.C / Jour</td>';

$FpRRow = '<td class="itemLibelle">Frais de personnel - Restaurant</td>';
$PcRow = '<td class="itemLibelle">Pertes connues</td>';
$RpRow = '<td class="itemLibelle">Repas du personnel</td>';
$ARow = '<td class="itemLibelle">Alimentaire</td>';
$ALRow = '<td class="itemLibelle">Alimentaire - Laboratoire</td>';
$TARow = '<td class="itemLibelle">Total Alimentaire</td>';
$ERow = '<td class="itemLibelle">Emballages</td>';

$txFpRRow = '<td class="itemLibelle">Taux Frais de personnel - Restaurant</td>';
$txPcRow = '<td class="itemLibelle">Taux Pertes connues</td>';
$txRpRow = '<td class="itemLibelle">Taux Repas du personnel</td>';
$txARow = '<td class="itemLibelle">Taux Alimentaire</td>';
$txALRow = '<td class="itemLibelle">Taux Alimentaire - Laboratoire</td>';
$txTARow = '<td class="itemLibelle">Taux Alimentaire (Total)</td>';
$txERow = '<td class="itemLibelle">Taux Emballages</td>';
foreach($sites as $site) {
    if($site->id != 0) {
        if(isset($monthBudget[$site->id])) {
            $nbWeek1Value = $monthBudget[$site->id]['nbWeek1'];
            $nbWeek2Value = $monthBudget[$site->id]['nbWeek2'];
            $nbWeek3Value = $monthBudget[$site->id]['nbWeek3'];
            $nbWeek4Value = $monthBudget[$site->id]['nbWeek4'];
            $nbWeek5Value = $monthBudget[$site->id]['nbWeek5'];
            $nbWeek6Value = $monthBudget[$site->id]['nbWeek6'];
            $nbopendaysValue = $nbWeek1Value+$nbWeek2Value+$nbWeek3Value+$nbWeek4Value+$nbWeek5Value+$nbWeek6Value;
            $tmJValue = $monthBudget[$site->id]['tm'];
            $txFpRValue = $monthBudget[$site->id]['txFpR'];
            $txPcValue = $monthBudget[$site->id]['txPc'];
            $txRpValue = $monthBudget[$site->id]['txRp'];
            $txAValue = $monthBudget[$site->id]['txA'];
            $txALValue = $monthBudget[$site->id]['txAl'];
            $txTAValue = $txAValue + $txALValue;
            $txEValue = $monthBudget[$site->id]['txE'];
            
            $totttcJValue = $monthBudget[$site->id]['totttc'];
            $totttcValue = $monthBudget[$site->id]['totttc']*$nbopendaysValue;
            $tothtValue = ($totttcValue*0.995/1.07)+($totttcValue*0.005/1.196);
            $transValue = ($tmJValue!=0)?$totttcValue/$tmJValue:0;
            $FpRValue = $tothtValue*$txFpRValue/100;
            $PcValue = $tothtValue*$txPcValue/100;
            $RpValue = $tothtValue*$txRpValue/100;
            $AValue = $tothtValue*$txAValue/100;
            $ALValue = $tothtValue*$txALValue/100;
            $TAValue = $AValue + $ALValue;
            $EValue = $tothtValue*$txEValue/100;
        } else {
            $nbWeek1Value = 0;
            $nbWeek2Value = 0;
            $nbWeek3Value = 0;
            $nbWeek4Value = 0;
            $nbWeek5Value = 0;
            $nbWeek6Value = 0;
            $nbopendaysValue = 0;
            $tmJValue = 0;
            $txFpRValue = 0;
            $txPcValue = 0;
            $txRpValue = 0;
            $txAValue = 0;
            $txALValue = 0;
            $txTAValue = 0;
            $txEValue = 0;
            
            $totttcJValue = 0;
            $totttcValue = 0;
            $tothtValue = 0;
            $transValue = 0;
            $FpRValue = 0;
            $PcValue = 0;
            $RpValue = 0;
            $AValue = 0;
            $ALValue = 0;
            $TAValue = 0;
            $EValue = 0;
        }
        
        $headerRow .= '<th>Site '.$site->id.'</th>';
        $nbopendaysRow .= '<td class="itemValue">'.$nbopendaysValue.'</td>';
        $nbWeek1Row .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="nbWeek1"
                data-type="text">'.$nbWeek1Value.'</a></td>';
        $nbWeek2Row .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="nbWeek2"
                data-type="text">'.$nbWeek2Value.'</a></td>';
        $nbWeek3Row .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="nbWeek3"
                data-type="text">'.$nbWeek3Value.'</a></td>';
        $nbWeek4Row .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="nbWeek4"
                data-type="text">'.$nbWeek4Value.'</a></td>';
        $nbWeek5Row .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="nbWeek5"
                data-type="text">'.$nbWeek5Value.'</a></td>';
        $nbWeek6Row .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="nbWeek6"
                data-type="text">'.$nbWeek6Value.'</a></td>';
        $totttcJRow .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="totttc"
                data-type="text">'.number_format($totttcJValue, 0, ',', ' ').'</a></td>';
        $totttcRow .= '<td class="itemValue">'.number_format($totttcValue, 0, ',', ' ').'</td>';
        $tothtRow .= '<td class="itemValue">'.number_format($tothtValue, 0, ',', ' ').'</td>';
        $transactionRow .= '<td class="itemValue">'.number_format($transValue, 0, ',', ' ').'</td>';
        $tmJRow .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="tm"
                data-type="text">'.number_format($tmJValue, 2, ',', ' ').'</a></td>';
        
        $FpRRow .= '<td class="itemValue">'.number_format($FpRValue, 0, ',', ' ').'</td>';
        $PcRow .= '<td class="itemValue">'.number_format($PcValue, 0, ',', ' ').'</td>';
        $RpRow .= '<td class="itemValue">'.number_format($RpValue, 0, ',', ' ').'</td>';
        $ARow .= '<td class="itemValue">'.number_format($AValue, 0, ',', ' ').'</td>';
        $ALRow .= '<td class="itemValue">'.number_format($ALValue, 0, ',', ' ').'</td>';
        $TARow .= '<td class="itemValue">'.number_format($TAValue, 0, ',', ' ').'</td>';
        $ERow .= '<td class="itemValue">'.number_format($EValue, 0, ',', ' ').'</td>';

        $txFpRRow .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="txFpR"
                data-type="text">'.number_format($txFpRValue, 2, ',', ' ').'</a></td>';
        $txPcRow .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="txPc"
                data-type="text">'.number_format($txPcValue, 2, ',', ' ').'</a></td>';
        $txRpRow .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="txRp"
                data-type="text">'.number_format($txRpValue, 2, ',', ' ').'</a></td>';
        $txARow .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="txA"
                data-type="text">'.number_format($txAValue, 2, ',', ' ').'</a></td>';
        $txALRow .= '<td class="itemValue">
            <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="txAl"
                data-type="text">'.number_format($txALValue, 2, ',', ' ').'</a></td>';
        $txTARow .= '<td class="itemValue">'.number_format($txTAValue, 2, ',', ' ').'</td>';
        $txERow .= '<td class="itemValue">
                <a class="editable" 
                data-original-title="Saisissez une valeur" 
                data-pk="'.$site->id.'_'.$month.'" 
                data-name="txE"
                data-type="text">'.number_format($txEValue, 2, ',', ' ').'</a></td>';
    }
}
?>
<style type="text/css">
    #budget-month-container .editable {
        border: 0;
        color: #000000;
        text-decoration: none;
    }
</style>
<script type="text/javascript">
    $(function(){
        // Manage Uri
        var uri = document.location.pathname;
        
        //editables 
        $.fn.editable.defaults.mode = 'popup';  
        $.fn.editable.defaults.emptytext = '0'; 
        $.fn.editable.defaults.url = uri+'/save';
        $.fn.editable.defaults.placement = 'right';
        
        $('.editable').editable({
            validate: function(v){
                if(!v) {
                    return 'Saisissez un chiffre';
                } else {
                    v = v.replace(' ', '');
                    v = v.replace(',', '.');
                    if(isNaN(v) == true) {
                        return 'Saisissez un chiffre';
                    }
                }
            }
        });
    });
</script>
<div class="list-container">
<table class="table table-bordered table-condensed table-hover">
    <tr class="headerRow"><?php echo $headerRow; ?></tr>
    <tr class="itemRow"><?php echo $nbopendaysRow; ?></tr>
    <tr class="itemRow"><?php echo $nbWeek1Row; ?></tr>
    <tr class="itemRow"><?php echo $nbWeek2Row; ?></tr>
    <tr class="itemRow"><?php echo $nbWeek3Row; ?></tr>
    <tr class="itemRow"><?php echo $nbWeek4Row; ?></tr>
    <tr class="itemRow"><?php echo $nbWeek5Row; ?></tr>
    <tr class="itemRow"><?php echo $nbWeek6Row; ?></tr>
    <tr class="itemRow unlocked"><?php echo $totttcJRow; ?></tr>
    <tr class="itemRow"><?php echo $totttcRow; ?></tr>
    <tr class="itemRow"><?php echo $tothtRow; ?></tr>
    <tr class="itemRow"><?php echo $transactionRow; ?></tr>
    <tr class="itemRow unlocked"><?php echo $tmJRow; ?></tr>
    
    <tr class="itemRow"><?php echo $FpRRow; ?></tr>
    <tr class="itemRow"><?php echo $PcRow; ?></tr>
    <tr class="itemRow"><?php echo $RpRow; ?></tr>
    <tr class="itemRow"><?php echo $ARow; ?></tr>
    <tr class="itemRow"><?php echo $ALRow; ?></tr>
    <tr class="itemRow"><?php echo $TARow; ?></tr>
    <tr class="itemRow"><?php echo $ERow; ?></tr>
    
    <tr class="itemRow unlocked"><?php echo $txFpRRow; ?></tr>
    <tr class="itemRow unlocked"><?php echo $txPcRow; ?></tr>
    <tr class="itemRow unlocked"><?php echo $txRpRow; ?></tr>
    <tr class="itemRow unlocked"><?php echo $txARow; ?></tr>
    <tr class="itemRow unlocked"><?php echo $txALRow; ?></tr>
    <tr class="itemRow"><?php echo $txTARow; ?></tr>
    <tr class="itemRow unlocked"><?php echo $txERow; ?></tr>
</table>
</div>